{% extends 'layout/manage.html' %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.min.css' %}">
    <style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-body {
            padding: 0;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .title-list {
            border-right: 1px solid #ddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .editormd-fullscreen{
            z-index: 1001;
        }

        .content {
            border-left: 1px solid #ddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <a href=""><i class="fa fa-book" aria-hidden="true"></i> wiki文档</a>
                </div>
                <div class="function">
                    <a href="" class="btn btn-primary btn-xs"><i class="fa fa-plus-circle" aria-hidden="true"></i>
                        新建</a>
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">

                    </ul>
                </div>
                <div class="col-sm-9 content" style="margin-top: 50px;">
                    <form method="post">
                        {% csrf_token %}
                        {% for form_obj in form %}
                            {% if form_obj.name == "content" %}
                                <div class="form-group">
                                    <label for="{{ form_obj.id_for_label }}">{{ form_obj.label }}</label>
                                    <div id="editor">
                                        {{ form_obj }}
                                    </div>
                                    <span class="error-msg">{{ form_obj.errors.0 }}</span>
                                </div>
                            {% else %}
                                <div class="form-group">
                                    <label for="{{ form_obj.id_for_label }}">{{ form_obj.label }}</label>
                                    {{ form_obj }}
                                    <span class="error-msg">{{ form_obj.errors.0 }}</span>
                                </div>
                            {% endif %}
                        {% endfor %}
                        <button type="submit" class="btn btn-primary">提 交</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}


{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script>
        var WIKi_DETAIL_URL = "{% url 'wiki' request.tracer.project.id %}";
        var IMAGE_UPLOAD_URL = "{% url 'wiki_upload' request.tracer.project.id %}";
        $(function () {
            init_catalog();
            init_editmd();
        })

        function init_editmd() {
            editormd("editor", {
               placeholder: "请输入内容",
                height: 500,
                path: "{% static 'plugin/editor-md/lib/' %}", // Autoload modules mode, codemirror, marked... dependents libs path
                imageUpload: true,
                imageFormats: ['jpg','png','gif','jpeg'],
                imageUploadURL: IMAGE_UPLOAD_URL,
            });
        }

        function init_catalog() {
            $.ajax({
                url: "{% url 'init_catalog' request.tracer.project.id %}",
                type: 'GET',
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        console.log(res.data)
                        $.each(res.data, function (index, item) {
                            var href = WIKi_DETAIL_URL + "?wiki_id=" + item.id;
                            var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").attr("href", href).text(item.title)).append($("<ul>"));
                            if (!item.parent_id) {
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li)
                            }
                        });
                    } else {
                        alert("初始化目录失败")
                    }
                }
            });
        }
    </script>
{% endblock %}